package ch02.Jianda;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class Jianda2 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse("src/ch02/scores.xml");

        NodeList scoresList = doc.getElementsByTagName("student");
        for (int i = 0; i < scoresList.getLength(); i++) {
            Element item = (Element) scoresList.item(i);
            String id = item.getAttribute("id");
            System.out.println("id："+id);
            NodeList childNodes = item.getChildNodes();
            for (int i1 = 0; i1 < childNodes.getLength(); i1++) {
                if (childNodes.item(i1).getNodeType() == Node.ELEMENT_NODE) {
                    Element item1 = (Element) childNodes.item(i1);
                    String textContent = item1.getTextContent();
                    System.out.println("\t"+textContent);
                }
            }
            System.out.println();

        }
    }
}
